<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/11/6
 * Time: 11:01
 */
namespace app\shop\controller;
use think\Db;
use think\Request;
class Role extends Base{

    public function rolePage(){
        $data = Db::name('role')
            ->where('role_s_id',session('shop_id'))
            ->order('role_id desc')
            ->select();
        foreach ($data as $key=>&$vo){
            $num  = Db::table('up_role_name')->where('f_id',$vo['role_id'])->count();
            $vo['num'] = $num;
        }
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function add(){
        return $this->fetch();
    }

    public function releaseTask(){
        if(request ()->isPost()){
            $data['role_name'] = input('param.keyword');
            $data['role_rule'] = implode(',',input('param.role/a'));
            $data['role_time'] = date('Y-m-d H:i:s');
            $data['role_s_id'] = session('shop_id');
            Db::table('up_role')->insert($data);
            return json(['status' => 200, 'msg' => '成功']);
        }
    }

    public function update(){
        $id = input('param.id');
        $data = Db::table('up_role')->where('role_id',$id)->find();
        $data['role_rule'] = explode(',',$data['role_rule']);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function updateTask(){
        if(request ()->isPost()){
            $id = input('param.id');
            $data['role_name'] = input('param.keyword');
            $data['role_rule'] = implode(',',input('param.role/a'));
            $data['role_time'] = date('Y-m-d H:i:s');
            Db::table('up_role')->where('role_id',$id)->update($data);
            return json(['status' => 200, 'msg' => '成功']);
        }
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $id = input('param.id');
        $res = Db::table('up_role')->where('role_id',$id)->delete();
        $info = Db::table('up_role_name')->where('f_id',$id)->find();
        if($info['role_name_id']!=''){
             Db::table('up_role_name')->where('f_id',$id)->delete();
        }
        if($res){
            return json(['status'=>200,'msg'=>'删除成功']);
        }else{
            return json(['status'=>100,'msg'=>'删除失败']);
        }

    }
}